package com.cadre.web.controller.officer;

import cn.hutool.core.util.RandomUtil;
import com.cadre.common.utils.Office2PDF;
import com.cadre.common.utils.common.DateUtils;
import com.cadre.common.utils.oldutil.DateUtil;
import com.cadre.common.utils.oldutil.*;
import com.cadre.system.domain.officer.A01;
import com.cadre.system.domain.officer.PerSonRenmVO;
import com.cadre.system.service.A01Service;
import com.cadre.system.service.NEWConfernamelistService;
import com.cadre.system.service.PptService;
import com.cadre.system.service.ReportRenmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RequestMapping({"/report/renm"})
@Api(value = "上会任免报表接口", tags = {"上会任免报表接口--成都"})
@RestController
/* loaded from: ReportRenmController.class */
public class ReportRenmController {
    @Autowired
    private ReportRenmService reportRenmService;
    @Autowired
    private ReportConfig config;
    @Autowired
    private A01Service a01Ser;
    @Autowired
    private NEWConfernamelistService nclSer;
    @Autowired
    private PptService pptSer;

    @PostMapping({"/list"})
    @ApiImplicitParam(name = "ids", value = "人员id", required = true, paramType = "query")
    @ApiOperation("上会任免报表：上会任免报表页面列表")
    public ResultVO selectPersonListById(String ids) {
        try {
            return this.reportRenmService.selectPerSonRenmListById(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.ERROR);
        }
    }

    @PostMapping({"/excel"})
    @ApiImplicitParam(name = "ids", value = "人员id", required = true, paramType = "query")
    @ApiOperation("上会任免报表：导出上会任免报表excel")
    public ResultVO exportExcel(String ids, HttpServletResponse httpServletResponse) {
        try {
            exportExcel((List) this.reportRenmService.selectPerSonRenmListById(ids).getData(), httpServletResponse);
            return ResultVOUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.ERROR);
        }
    }

    @PostMapping({"/pdf"})
    @ApiImplicitParam(name = "ids", value = "人员id", required = true, paramType = "query")
    @ApiOperation("上会任免报表：打印pdf")
    public ResultVO exportPdf(String ids, HttpServletResponse response, HttpServletRequest request) {
        try {
            exportPdf((List) this.reportRenmService.selectPerSonRenmListById(ids).getData(), response);
            return ResultVOUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.ERROR);
        }
    }

    @PostMapping({"/updateA01"})
    @ApiImplicitParam(name = "map", value = "干部人员id,NRZW,NMZW", required = true, paramType = "query", defaultValue = "{flowid:\"\",nrzw:\"\",nmzw:\"\"}")
    @ApiOperation("上会任免：更新拟任-拟免")
    public ResultVO updateA01(@RequestParam Map<String, Object> map) {
        if (map.get("flowid") == null || map.get("flowid").toString() == "") {
            return ResultVOUtil.error(100, "人员id为null,不能更新！");
        }
        A01 a = (A01) this.a01Ser.getById(map.get("flowid").toString());
        if (a == null) {
            return ResultVOUtil.error(100, "人员id传值错误！");
        }
        a.setNrzw(map.get("nrzw").toString());
        a.setNmzw(map.get("nmzw").toString());
        this.a01Ser.updateById(a);
        return ResultVOUtil.success();
    }

    private void exportExcel(List<PerSonRenmVO> list, HttpServletResponse httpServletResponse) throws IOException {
        PoiUtil.writeExcel(buildExcel(list), httpServletResponse, "干部任免上会表[" + new SimpleDateFormat("yyyy.MM.dd").format(new Date()) + "]");
    }

    @PostMapping({"/ppt"})
    @ApiImplicitParam(name = "ids", value = "人员id", required = true, paramType = "query")
    @ApiOperation("上会任免报表：导出上会任免报表ppt")
    public void exportPpt(String ids, String batchId, String flag, HttpServletResponse httpServletResponse) {
        String[] arr = ids.split(",");
        List<Map<String, Object>> listMap = new ArrayList<>();
        List<A01> list = this.nclSer.selectA01ListByPerSonIds(Arrays.asList(arr), batchId, flag);
        SimpleDateFormat df = new SimpleDateFormat(DateUtil.MONTG_DATE_FORMAT);
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                if (isNull(list.get(i).getA0101())) {
                    map.put("name", list.get(i).getA0101());
                } else {
                    map.put("name", "");
                }
                if (list.get(i).getA0171() != null) {
                    map.put("zp", list.get(i).getA0171());
                } else {
                    map.put("zp", new byte[10]);
                }
                StringBuffer sb = new StringBuffer();
                if (list.get(i).getA0101() == null || list.get(i).getA0101().trim().equals("")) {
                    sb.append("");
                } else {
                    sb.append(list.get(i).getA0101() + ",");
                }
                if (list.get(i).getA0105() == null || list.get(i).getA0105().trim().equals("") || !list.get(i).getA0105().equals("女")) {
                    sb.append("");
                } else {
                    sb.append(list.get(i).getA0105() + ",");
                }
                if (list.get(i).getA0130() == null || list.get(i).getA0130().trim().equals("") || list.get(i).getA0130().equals("汉族")) {
                    sb.append("");
                } else {
                    sb.append(list.get(i).getA0130() + ",");
                }
                if (isNull(list.get(i).getA0183())) {
                    sb.append("现任" + list.get(i).getA0183());
                    if (isNull(list.get(i).getNrzw()) || isNull(list.get(i).getNmzw())) {
                        sb.append(",");
                    } else {
                        sb.append("。");
                    }
                }
                if (isNull(list.get(i).getNrzw())) {
                    sb.append("拟任" + list.get(i).getNrzw());
                    if (isNull(list.get(i).getNmzw())) {
                        sb.append(",");
                    } else {
                        sb.append("。");
                    }
                }
                if (isNull(list.get(i).getNmzw())) {
                    sb.append("拟免" + list.get(i).getNmzw() + "。");
                }
                if (list.get(i).getA0106() != null) {
                    sb.append(df.format(list.get(i).getA0106()) + "生,");
                }
                if (isNull(list.get(i).getA0108())) {
                    sb.append(list.get(i).getA0108() + "人,");
                }
                if (isNull(list.get(i).getA0187())) {
                    sb.append("全日制" + list.get(i).getA0187());
                    if (!isNull(list.get(i).getA0188())) {
                        sb.append(",");
                    }
                }
                if (isNull(list.get(i).getA0188())) {
                    sb.append("  (" + list.get(i).getA0188() + "),");
                }
                if (isNull(list.get(i).getA0192())) {
                    sb.append(list.get(i).getA0192() + "。");
                }
                if (list.get(i).getA0180() != null) {
                    sb.append(df.format(list.get(i).getA0180()) + "任现职。");
                }
                if (list.get(i).getA0177b() != null) {
                    String[] strArr = list.get(i).getA0177b().replaceAll(" ", "").replaceAll("\n\r|\r\n|\n", "\r").split("\r");
                    for (String string : strArr) {
                        if (string.length() > 0) {
                            sb.append(string + "\n");
                        }
                    }
                }
                System.out.println(sb.toString());
                if (sb.length() > 1) {
                    map.put("grjj", sb.toString());
                } else {
                    map.put("grjj", "");
                }
                if (isNull(list.get(i).getA0196())) {
                    map.put("xz", list.get(i).getA0196().toString());
                } else {
                    map.put("xz", "");
                }
                listMap.add(map);
            }
            this.pptSer.inputDataForPpt(listMap, arr.length, httpServletResponse);
        }
    }

    private boolean isNull(String str) {
        if (str == null || str.trim().equals("")) {
            return false;
        }
        return true;
    }

    private void exportPdf(List<PerSonRenmVO> list, HttpServletResponse httpServletResponse) {
        Workbook workbook = buildExcel(list);
        String file = this.config.getFile() + RandomUtil.getRandom() + "/";
        String dateStr = "干部任免上会表[" + new SimpleDateFormat("yyyy.MM.dd").format(new Date()) + "]";
        File folder = new File(file);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String url = file + dateStr + ".xlsx";
        String pdfUrl = file + dateStr + ".pdf";
        PoiUtil.writeExcel(workbook, url);
        Office2PDF.office2Pdf(url, pdfUrl, this.config);
        WordUtils.toDownload(new File(pdfUrl), httpServletResponse);
    }

    private Workbook buildExcel(List<PerSonRenmVO> list) {
        Row row;
        Workbook workbook = PoiUtil.createWorkbook(this.config.getExcelShTmp());
        Sheet sheet = workbook.getSheetAt(0);
        Row rowstyle = sheet.getRow(2);
        int firstcellnum = rowstyle.getFirstCellNum();
        int lastcellnum = rowstyle.getLastCellNum();
        Cell cell = rowstyle.getCell(1);
        CellStyle style = workbook.createCellStyle();
        style.setWrapText(true);
        style.setAlignment(HorizontalAlignment.forInt( 2));
        style.setVerticalAlignment(VerticalAlignment.forInt( 1));
        style.setBorderBottom(cell.getCellStyle().getBorderBottom());
        style.setBorderLeft(cell.getCellStyle().getBorderLeft());
        style.setBorderRight(cell.getCellStyle().getBorderRight());
        style.setBorderTop(cell.getCellStyle().getBorderTop());
        Font chFont = workbook.createFont();
        chFont.setFontHeightInPoints((short) 12);
        chFont.setFontName("楷体_GB2312");
        Font ehFont = workbook.createFont();
        ehFont.setFontName("Times New Roman");
        ehFont.setFontHeightInPoints((short) 12);
        style.setFont(ehFont);
        for (int i = 0; i < list.size(); i++) {
            if (i != 0) {
                row = sheet.createRow(i + 2);
                row.setHeight(rowstyle.getHeight());
            } else {
                row = rowstyle;
            }
            int count = 1 + 1;
            row.createCell(1).setCellValue(setHSSStyle(list.get(i).getA0101(), chFont, ehFont));
            int count2 = count + 1;
            row.createCell(count).setCellValue(setHSSStyle(list.get(i).getA0183(), chFont, ehFont));
            int count3 = count2 + 1;
            row.createCell(count2).setCellValue(setHSSStyle(list.get(i).getNrzw(), chFont, ehFont));
            int count4 = count3 + 1;
            row.createCell(count3).setCellValue(setHSSStyle(list.get(i).getNmzw(), chFont, ehFont));
            int count5 = count4 + 1;
            row.createCell(count4).setCellValue(setHSSStyle(DateUtils.format(list.get(i).getA0185(), DateUtil.MONTG_DATE_FORMAT), chFont, ehFont));
            int count6 = count5 + 1;
            row.createCell(count5).setCellValue(setHSSStyle(list.get(i).getAa02(), chFont, ehFont));
            int count7 = count6 + 1;
            row.createCell(count6).setCellValue(setHSSStyle(list.get(i).getAa01(), chFont, ehFont));
            int count8 = count7 + 1;
            row.createCell(count7).setCellValue(setHSSStyle(list.get(i).getA0108(), chFont, ehFont));
            int count9 = count8 + 1;
            row.createCell(count8).setCellValue(setHSSStyle(DateUtils.format(list.get(i).getA0138(), DateUtil.MONTG_DATE_FORMAT), chFont, ehFont));
            int count10 = count9 + 1;
            row.createCell(count9).setCellValue(setHSSStyle(list.get(i).getAa03(), chFont, ehFont));
            int count11 = count10 + 1;
            row.createCell(count10).setCellValue(setHSSStyle(list.get(i).getA0187(), chFont, ehFont));
            int count12 = count11 + 1;
            row.createCell(count11).setCellValue(setHSSStyle(list.get(i).getA0188(), chFont, ehFont));
            int count13 = count12 + 1;
            row.createCell(count12).setCellValue(setHSSStyle(list.get(i).getA0190(), chFont, ehFont));
            int count14 = count13 + 1;
            row.createCell(count13).setCellValue(setHSSStyle(list.get(i).getA0191(), chFont, ehFont));
            int count15 = count14 + 1;
            row.createCell(count14).setCellValue(setHSSStyle(list.get(i).getRemark(), chFont, ehFont));
            for (int j = firstcellnum; j < lastcellnum; j++) {
                row.getCell(j).setCellStyle(style);
            }
        }
        return workbook;
    }

    public XSSFRichTextString setHSSStyle(String value, Font chfont, Font enfont) {
        if (value == null || "".equals(value)) {
            return null;
        }
        XSSFRichTextString ts = new XSSFRichTextString(value);
        ts.applyFont(chfont);
        Matcher matcher = Pattern.compile("(\\d{0,4}\\.?(\\d{0,2}))").matcher(value);
        int i = 0;
        while (matcher.find()) {
            i++;
            String str = matcher.group();
            if (!str.equals("")) {
                ts.applyFont(i - 1, (i + str.length()) - 1, enfont);
            }
        }
        return ts;
    }
}
